package com.zeng.utils;

import org.apache.tomcat.jni.Time;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TimeUtils {
    public static String NowTime(){
        Date date=new Date();   //这里的时util包下的
        SimpleDateFormat temp=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  //这是24时
        String Date=temp.format(date);
        return Date;
    }
    public static Date NowTimeN(){
        Date date=new Date();   //这里的时util包下的
        return date;
    }
    public static String DateToString(Date d){

        SimpleDateFormat temp=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  //这是24时
        String Da=temp.format(d);
        return Da;
    }
    public static Date StringToDate(String s){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date=new Date();
        try {
            // 注意格式需要与上面一致，不然会出现异常
            date = sdf.parse(s);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
    public static int minsBetween(Date date1, Date date2) {
        Calendar cal = Calendar.getInstance();
        if (date1 == null || date2 == null) {
            return 0;
        }
        cal.setTime(date1);
        long time1 = cal.getTimeInMillis();
        cal.setTime(date2);
        long time2 = cal.getTimeInMillis();
        return Math.abs(Integer.parseInt(String.valueOf((time2 - time1) / 60000L)) + 1);
    }


    public static void main(String[] args) {

        Date d = NowTimeN();

        System.out.println(minsBetween(NowTimeN(),StringToDate("2021-03-23 15:33:20")));

    }
}
